package com.android.bookgarden.utli;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * sp操作的工具类 1. 保存数据: key---value 2. 得到数据: 根据key取对应的value 3. 移除数据:
 * 根据key删除对应的key--vlaue
 * 
 * @author sss
 * 
 */
public final class SpUtils {

	public static SharedPreferences sp;
	private static SpUtils instance = null;
	private String fileName = "UserInfo";
	private String userid ;

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public synchronized static SpUtils getInstance() {
		//Log.i("userid", "userid: "+ Utils.USERNAME);
		if(null == instance)
		{
			instance = new SpUtils();
		}
		return instance;
	}


	/**
	 * 保存数据: key---value
	 *
	 * @param key
	 * @param value
	 *            String, int , boolean
	 */
	public void save(Context context , String key, Object value) {
		SharedPreferences sp =context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
		if (value instanceof String) {
			sp.edit().putString(key, (String) value).commit();
		} else if (value instanceof Integer) {
			sp.edit().putInt(key, (Integer) value).commit();
		} else if (value instanceof Boolean) {
			sp.edit().putBoolean(key, (Boolean) value).commit();
		}
	}
	/**
	 * 保存数据: key---value
	 *
	 * @param key
	 * @param value
	 *            String, int , boolean
	 */
	public void save(Context context , String fileName, String key, Object value) {
		SharedPreferences sp =context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
		if (value instanceof String) {
			sp.edit().putString(key, (String) value).commit();
		} else if (value instanceof Integer) {
			sp.edit().putInt(key, (Integer) value).commit();
		} else if (value instanceof Boolean) {
			sp.edit().putBoolean(key, (Boolean) value).commit();
		}
	}

	/**
	 * 得到字符串数据: 根据key取对应的value
	 *
	 * @param key
	 * @return
	 */
	public String getString(Context context , String key, String defValue) {
		SharedPreferences sp =context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
		return sp.getString(key, defValue);
	}
	/**
	 * 得到字符串数据: 根据key取对应的value
	 *
	 * @param key
	 * @return
	 */
	public String getString(Context context , String fileName, String key, String defValue) {
		SharedPreferences sp =context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
		return sp.getString(key, defValue);
	}

	/**
	 * 得到int数据: 根据key取对应的value
	 *
	 * @param key
	 * @return
	 */
	public int getInt(Context context , String key, int defValue) {
		SharedPreferences sp =context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
		return sp.getInt(key, defValue);
	}

	/**
	 * 得到boolean数据: 根据key取对应的value
	 *
	 * @param key
	 * @return
	 */
	public boolean getBoolean(Context context , String key, boolean defValue) {
		SharedPreferences sp =context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
		return sp.getBoolean(key, defValue);
	}

	/**
	 * 移除数据: 根据key删除对应的key--vlaue
	 *
	 * @param key
	 */
	public void remove(Context context , String key) {
		SharedPreferences sp =context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
		sp.edit().remove(key).commit();
	}




}
